﻿namespace Jhong.FunMVC
{
    using System;
    using System.Collections.Generic;

    internal static class ResponseCache
    {
        private static readonly IDictionary<string, Tuple<DateTime, Response>> _responseCach = new Dictionary<string, Tuple<DateTime, Response>>();

        private static object _lock = new object();

        public static Response GetCache(string url)
        {
            lock (_lock)
            {
                if (false == _responseCach.ContainsKey(url)) return null;
                var rep = _responseCach[url];
                if (rep.Item1 < DateTime.Now) return null;
                return rep.Item2;
            }
        }

        public static void AddCache(string url, Response response, int duration)
        {
            lock (_lock)
            {
                if (_responseCach.ContainsKey(url)) _responseCach.Remove(url);
                _responseCach.Add(url, new Tuple<DateTime, Response>(DateTime.Now.AddSeconds(duration), response));
            }
        }
    }
}